package com.ai.module.business.service.impl;

import com.ai.module.chat.common.base.ThreadLocalContext;
import com.ai.module.user.domain.entity.RobotGroupEntity;
import com.ai.module.user.domain.entity.UserRobotEntity;
import com.ai.module.user.domain.req.GroupRobotReq;
import com.ai.module.user.domain.vo.RobotGroupVo;
import com.ai.module.user.enu.UserOperateType;
import com.ai.module.user.mapper.RobotGroupMapper;
import com.ai.module.user.service.RobotGroupService;
import com.ai.module.user.service.UserRobotService;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author: heJinQin
 * @create: 2024-03-20 22:53
 * @description:
 **/
@Service
public class RobotGroupServiceImpl extends ServiceImpl<RobotGroupMapper, RobotGroupEntity> implements RobotGroupService {

    @Autowired
    private RobotGroupMapper groupMapper;
    @Autowired
    private UserRobotService userRobotService;

    @Override
    public List<String> findLabels() {
        return baseMapper.findLabels();
    }

    @Override
    public List<RobotGroupVo> userLabels() {
        String userId = ThreadLocalContext.getUserId();
        List<RobotGroupVo> robotGroupVos = groupMapper.userLabels(userId);
        return robotGroupVos;
    }

    @Override
    public void updateRobot(GroupRobotReq groupRobotReq) {
        UserOperateType operateType = groupRobotReq.getOperateType();
        String robotId = groupRobotReq.getRobotId();
        if (operateType == UserOperateType.DELETE){
            // 删除 用户的 AI
            userRobotService.remove(
                    new LambdaQueryWrapper<UserRobotEntity>()
                            .eq(UserRobotEntity::getSourceId,robotId)
            );
        }else {
            // 给用户新增 该机器人
            Integer robotType = groupRobotReq.getRobotType();
            String backGroundUrl = groupRobotReq.getBackGroundUrl();
            String imageUrl = groupRobotReq.getImageUrl();
            String robotName = groupRobotReq.getRobotName();
            UserRobotEntity userRobot = new UserRobotEntity();
            userRobot.setRobotType(robotType);
            userRobot.setBackGroundUrl(backGroundUrl);
            userRobot.setRobotName(robotName);
            userRobot.setImageUrl(imageUrl);
            userRobot.setUserId(ThreadLocalContext.getUserId());
            userRobot.setSourceId(robotId);
            userRobot.setIsDefault(0);
            userRobotService.save(userRobot);
        }

    }
}
